package cn.john.hrm.interceptor;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class AuthFeignInterceptor implements RequestInterceptor {
    private static String url = "http://localhost:3010/oauth/token?client_id=system&client_secret=1&grant_type=client_credentials&redirect_uri=http://www.baidu.com";

    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 使用客户端模式 发起请求 获取token
        String result = HttpUtil.get(url);
        // 将Json字符串转换为Java对象
        Map map = JSON.parseObject(result, Map.class);
        // 获取token
        String access_token = map.get("access_token").toString();
        System.err.println(access_token);
        // 将token设置到Feign发起的请求的请求头中
        requestTemplate.header("Authorization","bearer "+access_token);
    }
}
